import 'package:flutter/material.dart';
import 'package:flutter_09_game/base/widget/base_get_builder.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/config/app_routes.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/profile/profile_bet_record/widgets/widget_bet_record_top_crypto.dart';
import 'package:flutter_09_game/pages/promotion/widgets/promotion_transfer_bing.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';
import 'package:flutter_09_game/utils/formatters/decimal_input_formatter.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/widgets/appbar/common_app_bar.dart';
import 'package:flutter_09_game/widgets/button/common_button.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_09_game/widgets/input/common_text_field.dart';
import 'package:flutter_09_game/widgets/widget_profile_item.dart';
import 'package:flutter_09_game/widgets/widget_verification_box.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'promotion_transfer_commission_logic.dart';

class PromotionTransferCommissionPage extends StatelessWidget {
  PromotionTransferCommissionPage({super.key});

  final PromotionTransferCommissionLogic logic =
      Get.put(PromotionTransferCommissionLogic());

  @override
  Widget build(BuildContext context) {
    return BaseGetBuilder<PromotionTransferCommissionLogic>(
      appBar: CommonAppBar(title: LocaleKeys.transferToPlatformBalance.tr),
      builder: (l) {
        return Column(
          children: [
            // 16.verticalSpaceFromWidth,
            WidgetBetRecordTopCrypto(
                backgroundColor: Colors.transparent,
                selectorTagRx: logic.selectorTagRx,
                options: logic.options),
            // 16.verticalSpaceFromWidth,
            Expanded(
              child: Obx(() {
                return Container(
                  margin: EdgeInsets.all(16.w),
                  padding:
                      EdgeInsets.symmetric(vertical: 16.w, horizontal: 12.w),
                  decoration: BoxDecoration(
                      color: AppColor.navBgColor,
                      borderRadius: BorderRadius.circular(16.w)),
                  child: Column(
                    children: [
                      Text(
                        LocaleKeys.currentTransferableBalance.tr
                            .trArgs([logic.selectorTagRx.value]),
                        style: TextStyle(
                            color: AppColor.textPrimary.withOpacity(0.7),
                            fontSize: 14.sp,
                            fontWeight: FontWeight.w500),
                      ),
                      4.verticalSpaceFromWidth,
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            // AppUserManager().currentWalletRx.value
                            logic.currentWallet?.rebateBalance.formatWallet() ??
                                '',
                            // LocaleKeys.currentTransferableBalance.tr.trArgs(
                            //     [logic.selectorTagRx.value]),
                            style: TextStyle(
                                color: AppColor.textPrimary.withOpacity(0.94),
                                fontSize: 20.sp,
                                fontWeight: FontWeight.w900),
                          ),
                          8.horizontalSpace,
                          AutoImage(
                            UtilsOther()
                                .getAmountTagIcon(logic.selectorTagRx.value),
                            width: 20.w,
                          ),
                          4.horizontalSpace,
                          Text(
                            logic.selectorTagRx.value,
                            style: TextStyle(
                                color: AppColor.textPrimary.withOpacity(0.7),
                                fontSize: 14.sp,
                                fontWeight: FontWeight.w400),
                          ),
                        ],
                      ),
                      24.verticalSpaceFromWidth,
                      Expanded(
                          child: SingleChildScrollView(
                        child:
                            (AppUserManager().user.value?.hasBindTrc20Address ==
                                        true &&
                                    AppUserManager()
                                            .user
                                            .value
                                            ?.hasSetFundPassword ==
                                        true)
                                ? _buildMain()
                                : const PromotionTransferBing(),
                      )),
                      12.verticalSpaceFromWidth,
                      Visibility(
                          visible: AppUserManager()
                                      .user
                                      .value
                                      ?.hasBindTrc20Address ==
                                  true &&
                              AppUserManager().user.value?.hasSetFundPassword ==
                                  true,
                          child: CommonButton.expend(
                            title: LocaleKeys.ok.tr,
                            enable: logic.isFormValid.value,
                            onClick: () {
                              logic.postWalletRebateToBalance();
                            },
                          ))
                    ],
                  ),
                );
              }),
            ),
            // 16.verticalSpaceFromWidth,
          ],
        );
      },
    );
  }

  Widget _buildMain() {
    return Column(
      children: [
        WidgetProfileItem(
          LocaleKeys.transferInAmount.tr,
          CommonTextField(
            textEditingController: logic.editingController,
            backgroundColor: Colors.black.withOpacity(0.3),
            border: Border(
                bottom: BorderSide(
                    color: AppColor.textPrimary.withOpacity(0.15), width: 1.w)),
            // borderSide: BorderSide(
            //     color: AppColor.textPrimary.withOpacity(0.05), width: 1.w),
            hintText: LocaleKeys.enterTransferInAmount.tr,
            keyboardType: const TextInputType.numberWithOptions(decimal: true),
            inputFormatters: [DecimalInputFormatter(decimalPlaces: 8)],
            onChanged: (text) {
              logic.validateForm();
            },
          ),
          rightWidget: GestureDetector(
            onTap: () {
              logic.editingController.text =
                  // fixed: 3
                  logic.currentWallet?.rebateBalance.formatWallet(fixed: 3) ?? '';
            },

            child: Text(
              LocaleKeys.transferAllToPlatform.tr,
              style: TextStyle(
                color: AppColor.primary,
                fontSize: 12.sp,
              ),
            ),
          ),
        ),
        12.verticalSpaceFromWidth,
        WidgetProfileItem(
          LocaleKeys.fundPassword.tr,
          WidgetVerificationBox(
            verificationBoxController: logic.verificationBoxController,
            onValueChanged: (text) {
              logic.fundPassword.value = text;
            },
          ),
          rightWidget: GestureDetector(
            onTap: () {
              Get.toNamed(AppRoutes.profileFindFundPasswordPage)
                  ?.then((value) {});
            },
            child: Text(
              LocaleKeys.forgotFundPassword.tr,
              style: TextStyle(
                color: AppColor.primary,
                fontSize: 12.sp,
              ),
            ),
          ),
        )
      ],
    );
  }
}
